package com.ht.shop.dao.impl;

import com.ht.shop.dao.OrderDao;
import com.ht.shop.utils.DBUtils;
import com.ht.shop.utils.SqlUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

public class OrderDaoImpl implements OrderDao {
    private QueryRunner queryRunner = new QueryRunner(DBUtils.getSource());
    @Override
    public void addOrder(String oid,long uid, long money, String aid) {
        String sql = SqlUtils.getSql("order.add");
        try {
            queryRunner.insert(sql, new ScalarHandler<>(), oid,uid, money, aid);

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public long addOrderDetail(String oid, long kzid, int num, long money) {
        String sql = SqlUtils.getSql("orderdetail.add");
        try {
            long detailId= queryRunner.insert(sql, new ScalarHandler<>(), oid, kzid, num,money);
            return detailId;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void updateStaus2Success(String id) {
        String sql = SqlUtils.getSql("order.paysuccess");
        try {
            queryRunner.update(sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String findStatusById(String id) {
        String sql =SqlUtils.getSql("order.status");
        try {
            String result = queryRunner.query(sql, new ScalarHandler<>(), id);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
